1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * TimedInvocation.java
6    *
7    * Created on 7-Jan-2005 at 12:24:41 PM
8    */
9   package ca.uhn.cache.helper;
10  
11  import org.apache.commons.lang.time.StopWatch;
12  
13  
14  /***
15   * Times the invocation of the <code>doInvoke()</code> method.
16   *  
17   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
18   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:54:31 $ by $Author: bryan_tripp $
19   */
20  public abstract class InvocationTimer {
21      
22      private StopWatch myStopWatch;
23      
24      /***
25       */
26      public InvocationTimer() {
27          myStopWatch = new StopWatch();
28      }
29      
30      /***
31       * Entry point.
32       *  
33       * @return The return value of <code>doInvoke</code>.
34       * 
35       * @throws Exception Potential exception thrown by <code>doInvoke()</code>.
36       */
37      public Object invoke() throws Exception {
38          Object retVal;
39          
40          myStopWatch.start();
41          retVal = doInvoke();
42          myStopWatch.stop();
43          
44          return retVal;
45      }
46      
47      /***
48       * @return The time elapsed since the method invocation was fired.
49       */
50      public long getElapsedTime() {
51          return myStopWatch.getTime();
52      }
53      
54      /***
55       * The code whose execution we want to time.
56       * 
57       * @return The timed code return value.
58       * 
59       * @throws Exception ...
60       */
61      protected abstract Object doInvoke() throws Exception;
62      
63  }